package net.hn.hnms.biz.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.project.domain.ContractInfo;
import net.hn.hnms.biz.project.mapper.ContractInfoMapper;
import net.hn.hnms.biz.project.request.ContractInfoReq;
import net.hn.hnms.biz.project.service.ContractInfoService;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ContractInfoServiceImpl implements ContractInfoService {
    @Autowired
    private ContractInfoMapper contractInfoMapper;

    @Override
    public int insert(ContractInfoReq contractInfoReq) {
        ContractInfo insertObj = new ContractInfo();
        BeanUtil.copyProperties(contractInfoReq, insertObj);
        return contractInfoMapper.insert(insertObj);
    }

    @Override
    public void updateDeleteByIds(List<String> idList) {
        contractInfoMapper.updateDeleteByIds(idList);
    }

    @Override
    public void updateById(ContractInfoReq contractInfoReq) {
        ContractInfo updateObj = new ContractInfo();
        BeanUtil.copyProperties(contractInfoReq, updateObj);
        int updateCount = contractInfoMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
    }

    @Override
    public ContractInfo selectDetailById(String id) {
        ContractInfo contractInfo = contractInfoMapper.selectById(id);
        if (contractInfo == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        return contractInfo;
    }

    @Override
    public ResponsePage<ContractInfo> selectPageList(ContractInfoReq contractInfoReq, PageQuery pageQuery) {
        Page<ContractInfo> page = contractInfoMapper.selectPage(pageQuery.build(), buildQuery(contractInfoReq).orderByDesc(ContractInfo::getCreateTime));
//        List<WaterSupplyCompanyVo> list =page.getRecords().stream().map(o ->convert(o, WaterSupplyCompanyVo.class)).collect(Collectors.toList());
        return new ResponsePage<>(page.getRecords(), page.getTotal());
    }


    /**
     * 构造查询条件
     * @param req
     * @return LambdaQueryWrapper<ExecutionOther> 查询条件
     */
    private LambdaQueryWrapper<ContractInfo> buildQuery(ContractInfoReq req){
        return new LambdaQueryWrapper<ContractInfo>()
                .like(StrUtil.isNotBlank(req.getLegalPersonName()), ContractInfo::getLegalPersonName, req.getLegalPersonName())
                .eq(StrUtil.isNotBlank(req.getMineCode()), ContractInfo::getMineCode, req.getMineCode())
                .like(StrUtil.isNotBlank(req.getContractCode()), ContractInfo::getContractCode, req.getContractCode())
                .like(StrUtil.isNotBlank(req.getContractTitle()), ContractInfo::getContractTitle, req.getContractTitle())
                .ge(req.getTotalAmount() != null, ContractInfo::getTotalAmount, req.getTotalAmount())
                .ge(req.getBeginDateTime() != null, ContractInfo::getContractDate, req.getBeginDateTime())
                .le(req.getEndDateTime() != null, ContractInfo::getContractDate, req.getEndDateTime());
    }

    @Override
    public void updateStateStop(List<String> idList) {
        contractInfoMapper.updateStateStop(idList);
    }
}
